package com.bluestar.tools.module.main;

import android.Manifest;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.bluestar.tools.R;
import com.bluestar.tools.common.base.BaseActivity;
import com.bluestar.tools.module.camera.CameraActivity;
import com.bluestar.tools.module.footerview.FooterViewActivity;
import com.bluestar.tools.module.devicemanager.DeviceManagerActivity;
import com.bluestar.tools.module.systemsettings.SystemSettingsActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;

public class MainActivity extends BaseActivity implements MainView {

    private MainPresenter mMainPresenter;

    private QMUITopBarLayout mTopbar;
    private RecyclerView mRecyclerView;
    private MainAdapter mMainAdapter;

    private TextView mGetDirTv;
    private Handler mHandler = new Handler();

    private Disposable disposable;

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("data", mGetDirTv.getText().toString());
    }

    @Override
    protected int getLayout() {
        return R.layout.activiyt_main;
    }

    @Override
    protected void initTopbar() {
        mTopbar = findViewById(R.id.topbar);
        mTopbar.setTitle(R.string.app_name).setTextColor(ContextCompat.getColor(this, R.color.qmui_config_color_white));
        mTopbar.setBackgroundColor(ContextCompat.getColor(this, R.color.qmui_btn_blue_bg));
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mMainPresenter = new MainPresenter(this);

        mRecyclerView = findViewById(R.id.activity_main_recyclerView);

        // 获取屏幕方向
        int orientation = this.getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 横屏，垂直布局
            RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
            mRecyclerView.setLayoutManager(linearLayoutManager);

        } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            // 竖屏，网格布局
            RecyclerView.LayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
            mRecyclerView.setLayoutManager(gridLayoutManager);
        }

        mGetDirTv = findViewById(R.id.activity_main_get_dir_tv);
        mGetDirTv.setMovementMethod(ScrollingMovementMethod.getInstance());
        mMainPresenter.getMainList(this);

        if (savedInstanceState != null) {
            mGetDirTv.setText(savedInstanceState.getString("data"));
        }

        // Android 6.0相机动态权限检查
        RxPermissions rxPermissions = new RxPermissions(this);
        disposable = rxPermissions
            .request(Manifest.permission.READ_PHONE_STATE)
            .subscribe(granted -> {
                if (granted) { // Always true pre-M
                    // I can control the camera now
                    Log.i("permissions", Manifest.permission.CAMERA + "：获取成功");
                } else {
                    // Oups permission denied
                    Log.i("permissions", Manifest.permission.CAMERA + "：获取失败");
                    finish();
                }
            });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public Handler getHandler() {
        return mHandler;
    }

    @Override
    public void updateList(List<MainBean> mainList) {
        if (mMainAdapter == null) {
            mMainAdapter = new MainAdapter(mainList, mMainPresenter);
            mMainAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    switch (ItemType.getItemType(position)) {
                        case GO_TO_CAMERA:
                            goToCameraActivity();
                            break;

                        case GET_DIR:
                            mMainPresenter.getDir(MainActivity.this);
                            break;

                        case DEVICE_INFO:
                            mMainPresenter.getDeviceInfo();
                            break;

                        case CREATE_FILE:
                            mMainPresenter.createFile();
                            break;

                        case CAMERA_INFO:
                            mMainPresenter.checkCamera();
                            break;

                        case CHANGE_ORIENTATION:
                            changeOrientation();
                            break;

                        case BUGLY_CRASH:
                            mMainPresenter.sendBuglyCrash();
                            break;

                        case GET_IMEI:
                            mMainPresenter.getIMEI(MainActivity.this);
                            break;

                        case FOOTER_LIST:
                            gotoFooterView();
                            break;

                        case DEVICE_MANAGER:
                            deviceManager();
                            break;

                        case SYSTEM_SETTINGS:
                            systemSettings();

                        default:
                            break;
                    }
                }
            });
            mRecyclerView.setAdapter(mMainAdapter);
        } else {
            mMainAdapter.setList(mainList);
        }
    }

    @Override
    public void goToCameraActivity() {
        Intent intent = new Intent(MainActivity.this, CameraActivity.class);
        startActivity(intent);
    }

    @Override
    public void updateUI(String data) {
        mGetDirTv.setText(data);
    }

    @Override
    public void changeOrientation() {
        // 获取屏幕方向
        int orientation = getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 设置竖屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            // 设置横屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }

    @Override
    public void gotoFooterView() {
        Intent intent = new Intent(MainActivity.this, FooterViewActivity.class);
        startActivity(intent);
    }

    @Override
    public void deviceManager() {
        Log.d(TAG, "deviceManager: 设备管理");
        //getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        Intent intent = new Intent(MainActivity.this, DeviceManagerActivity.class);
        startActivity(intent);
    }

    @Override
    public void systemSettings() {
        Log.d(TAG, "systemSettings: 系统设置");
        Intent intent = new Intent(MainActivity.this, SystemSettingsActivity.class);
        startActivity(intent);
    }
}